<template>
  <el-dialog title="定时发布" :visible="openView" :before-close="handleClose" width="500px">
    <div class="time-wrapper">
      <div class="time-header">请选择当前时间后进行定时发布</div>
      <el-row :gutter="24" style="margin-bottom: 1rem;">
        <el-col :span="12">
          <el-date-picker v-model="date" type="date" placeholder="选择发布日期" value-format="yyyy-MM-dd"></el-date-picker>
        </el-col>
        <el-col :span="12">
          <el-time-picker v-model="time" placeholder="选择发布时间" value-format="HH:mm:ss"></el-time-picker>
        </el-col>
      </el-row>
      <div class="time-body" v-if="this.date">
        本文将于北京时间 <span style="color: #303133;">{{ date }} {{ time }}</span> 发布
      </div>
      <div slot="footer" class="time-footer">
        <el-button @click="handleClear()" size="small" icon="el-icon-error">取消</el-button>
        <el-button type="primary" @click="confirm()" size="small" icon="el-icon-success">定时发布</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'TimeRelease',

  data () {
    return {
      // 是否打开对话框
      openView: false,
      // 发布日期
      date: undefined,
      // 发布时间
      time: undefined
    }
  },
  methods: {
    // 打开对话框
    handleView () {
      this.openView = true
    },
    // 确定发布时间
    confirm () {
      if (!this.date) {
        this.$message.error('发布日期不能为空')
        return
      }
      if (!this.time) {
        this.$message.error('发布时间不能为空')
        return
      }
      const publishTime = this.date + ' ' + this.time
      // 将选中的时间发送给父组件
      this.$emit('confirm', publishTime)
    },
    // 关闭对话框
    handleClose () {
      this.openView = false
    },
    // 点击取消按钮
    handleClear () {
      this.date = undefined
      this.time = undefined
      this.handleClose()
    }
  }
}
</script>

<style lang="scss" scoped>
.time-wrapper {
  padding: 10px 30px;
}
.time-header {
  margin-bottom: 0.5rem;
  color: #303133;
}
.time-body {
  margin-bottom: 1rem;
  color: #909399;
}
.time-footer {
  text-align: right;
  margin-bottom: 1rem;
}
</style>
